package cn.wolfcode.consumer_test.api._03_consume_mode;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //创建一个消费者
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("wolfcode-consumer");
        //绑定namesrv地址
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //绑定主题
        defaultMQPushConsumer.subscribe("03-consumer-mode","*");
        //消费消息
        //defaultMQPushConsumer.setMessageModel(MessageModel.CLUSTERING);
        defaultMQPushConsumer.setMessageModel(MessageModel.BROADCASTING);
        //处理消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消费者1的消息是:" + new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //消费消息
        defaultMQPushConsumer.start();
    }
}
